package Logic;

import java.util.ArrayList;

import javax.swing.JLabel;

import Gui.frontend_main;

public class CollisionDetection {

	// TODO : need to get panelWidth/panelHeight from frontend_main in case panel is changed


	int panelWidth = 458;
	int panelHeight = 369;
	ArrayList<Ball> ballList = new ArrayList<Ball>();
	// final integer variables for levels
	//lower the number, the closer the balls are to one another
	final int L1 = 1;
	final int L2 = 2;
	final int L3 = 3;
	final int L4 = 4;


	/**
	 * This method checks for collisions
	 *
	 * @return
	 */
	public boolean Collision(ArrayList<Ball> balls) {
		this.ballList = balls;
		// variables to compare distance between circle's centers
		int dx = 50;
		int dy = 50;

		boolean coll = false;
		// compare distance
		if (ballList.size() == 1) {// if only one ball, return false
			return false;
		} else
			for (int i = 0; i < ballList.size() - 1; i++) {
				for (int j = 0; j < ballList.size(); j++) {
					// doesn't allow collision to check the same ball
					if (i == j) {
						j++;
					}

					if (dx >= Math.abs((ballList.get(i).getLabelX() -
							ballList.get(j).getLabelX()
							))) {
						if (dy >= Math.abs((ballList.get(i).getLabelY() -
								ballList.get(j).getLabelY()))) {
							coll = true;
						}
					}
				}
			}
		return coll;
	}

	/**
	 *
	 * @param ball1
	 * @param ball2
	 * @return level
	 *
	 *         this method finds the distance b/t the balls and classifies it as
	 *         a level. Level will be used for amount of movement.
	 */
	public int getXLevel(Ball ball1, Ball ball2) {
		Ball b1 = ball1;
		Ball b2 = ball2;



		if (Math.abs(b1.getLabelX() - b2.getLabelX()) < panelWidth / 4) {
			return L1;
		} else if (Math.abs(b1.getLabelX() - b2.getLabelX()) < panelWidth / 3) {
			return L2;
		} else if (Math.abs(b1.getLabelX() - b2.getLabelX()) < panelWidth / 2) {
			return L3;
		}
		else {
			return L4;
		}

	}

	/**
	 *
	 * @param ball1
	 * @param ball2
	 * @return level
	 * 			Y Coordinate Level
	 *         this method finds the distance b/t the balls and classifies it as
	 *         a level. Level will be used for amount of movement.
	 */
	public int getYLevel(Ball ball1, Ball ball2) {
		Ball b1 = ball1;
		Ball b2 = ball2;



		if (Math.abs(b1.getLabelY() - b2.getLabelY()) < panelHeight / 4) {
			return L1;
		} else if (Math.abs(b1.getLabelY() - b2.getLabelY()) < panelHeight / 3) {
			return L2;
		} else if (Math.abs(b1.getLabelY() - b2.getLabelY()) < panelHeight / 2) {
			return L3;
		}
		else {
			return L4;
		}

	}



}
